package com.glorysoft.sunnypda.ui.eqpregister

import android.app.Activity
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.RepairInfoEntity
import com.glorysoft.sunnypda.entity.ui.AddEqpEntity
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.model.EqpRepository
import com.glorysoft.sunnypda.ui.reportrepair.ReportRepairViewModel
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: EqpRegisterViewModel
 * Author: XJH
 * Date: 2022/1/7 17:36
 * Description:
 * History:
 */
class EqpRegisterViewModel : BaseViewModel() {

    val onClick : DeleteEqp = object : DeleteEqp{
        override fun onDelete(item: EqpInfoEntity) {
            eqpItems.remove(item)
        }

        override fun onClick(item: EqpInfoEntity) {
            var bundle = Bundle()
            bundle.putSerializable("DATA",item)
            bundle.putInt("TYPE",0)
            defUI.startActivity.value = bundle
        }

    }

    var eqpItems = ObservableArrayList<EqpInfoEntity>()
    var eqpBinding =
        ItemBinding.of<EqpInfoEntity>(BR.eqpItem, R.layout.item_add_eqp_register).bindExtra(BR.onDeleteClick,onClick)

    val eqpRepository : EqpRepository = EqpRepository()
    var fabList = arrayListOf<String>()
    var eqpTypeList = arrayListOf<String>()

    fun getFabList(){
        val map = JsonObject()
        launchOnlyResult({
            val allFab = eqpRepository.getAllFab(map)
            for (areaInfoEntity in allFab.FABLIST) {
                fabList.add(areaInfoEntity.FAB!!)
            }
            for (eqpType in allFab.EQPTYPELIST){
                eqpTypeList.add(eqpType.EQPTYPE!!)
            }
        })
    }

    fun getEqpInfoAdd(codeS: String) {
        val map = JsonObject()
        map.addProperty("EQPID",codeS)
        launchOnlyResult({
            val eqpInfoByFab = eqpRepository.getEqpInfoAdd(map)
            defUI.callObserve.value = CallObserveBean(0,eqpInfoByFab)
        })

    }

    fun addEqpToList(eqpInfoEntity: EqpInfoEntity) {
        eqpItems.forEach{
            if (it.EQPID == eqpInfoEntity.EQPID){
                defUI.toastEvent.value = ToastMessageBean("当前设备已添加",false)
                return
            }
        }
        eqpInfoEntity.NO = (eqpItems.size + 1).toString()
        eqpItems.add(eqpInfoEntity)
    }

    fun submit(context: Activity,bitmap : String) {
        val map = JsonObject()
        map.add("BodyEQPList", JsonParser().parse(Gson().toJson(eqpItems)).asJsonArray)
        map.addProperty("CONFIRMOP",bitmap)
        launchOnlyResult({
            eqpRepository.getEqpRegister(map)
            defUI.toastEvent.value = ToastMessageBean(context.getString(R.string.success),true)
            context.finish()
        })

    }

    interface DeleteEqp{
        fun onDelete(item : EqpInfoEntity)
        fun onClick(item : EqpInfoEntity)
    }
}